//
//  WHRegistController.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/4.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHRegistController.h"
#import "WHRegisterView.h"
#import "NSString+WH3DES.h"
#import "WHTextInfoController.h"
#import "WHInfoAuthController.h"

@interface WHRegistController ()

@property (nonatomic, strong) NSURLSessionDataTask *resetPasswordTask;
@property (nonatomic, strong) WHRegisterView *registerView;

@property (nonatomic, strong) NSURLSessionDataTask *registerTask;

@end

@implementation WHRegistController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"注册";
    [self setUpUI];
}

- (void)setUpUI {
    WHRegisterView *registerView = [[WHRegisterView alloc] init];
    [self.view addSubview:registerView];
    [registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(kTopHeight);
    }];
    kWeakSelf(self);
    registerView.doRegist = ^(NSArray * _Nonnull messageArray) {
        [weakself doRegist:messageArray];
    };
    registerView.acceptRule = ^{
        [weakself toRuleWeb];
    };
    _registerView = registerView;
    
}

- (void)toRuleWeb {
    WHTextInfoController *textInfo = [[WHTextInfoController alloc] init];
    textInfo.urlstr = URL_agreement;
    [self.navigationController pushViewController:textInfo animated:YES];
}

- (void)doRegist:(NSArray *)messageArray {
    DLog(@"doRegist = %@", messageArray);
    [self.view endEditing:YES];
    
    NSString *phoneNumber = messageArray[0];
    NSString *codeNumber = messageArray[1];
    NSString *password = messageArray[2];
    NSString *checkpassword = messageArray[3];
    
    if (phoneNumber.length == 0 || codeNumber.length == 0 || password.length == 0 || checkpassword.length == 0) {
        [WHView showMessage:@"请完善以上信息" inView:self.view];
        return;
    }
    if (codeNumber.length != 6) {
        [WHView showMessage:@"请输入6位验证码" inView:self.view];
        return;
    }
    if (![WHValidationHelper validatePassword:password]) {
        [WHView showMessage:@"密码输入不合法!" inView:self.view];
        return;
    }
    if (![password isEqualToString:checkpassword]) {
        [WHView showMessage:@"两次输入密码不一致，请重新输入" inView:self.view];
        return;
    }
    
    kWeakSelf(self);
    [self doRegist:messageArray completion:^(id responseObject) {
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                [WHView showHudMsg:@"注册成功"];
                
                NSDictionary *userModel = responseObject[@"retobj"];
                WHDetailUserInfo *detailUserInfo = [WHDetailUserInfo modelWithDictionary:userModel];
                userManager.detailUserInfo = detailUserInfo;
                
                [weakself toInfoAuth];
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    }];
}

- (void)toInfoAuth {
    WHInfoAuthController *infoAuth = [[WHInfoAuthController alloc] init];
    [self.navigationController pushViewController:infoAuth animated:YES];
    NSMutableArray *tempMarray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [tempMarray removeObject:self];
    [self.navigationController setViewControllers:tempMarray];
}

#pragma mark —————— 接口 ——————
/** 注册 */
- (void)doRegist:(NSArray *)messageArray completion:(completionBlock)completion {
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_userReg);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          messageArray[0], @"userCell",
                          messageArray[1], @"smsCode",
                          [NSString encryptUseDES3:messageArray[2] key:DES3Key], @"passWord",
                          nil];
    
    _registerTask = [WHNetworkManager requestWithURL:postURL parameters:dict isUpData:YES Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        completion(responseObject);
    } failure:^(NSError *error) {
        
    }];
}

- (void)backBtnClicked {
    [super backBtnClicked];
    if (_registerTask) {
        [_registerTask cancel];
        _registerTask = nil;
    }
    [_registerView cancelRequest];
}

@end
